package org.apak.frontend;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apak.db.RepairServiceDAO;
import org.apak.log.FileLogger;
import org.apak.model.Customer;
import org.apak.model.LoggedSubject;
import org.apak.model.ServiceOrder;

/**
 * Servlet implementation class CustomerMenuServlet
 */
public class CustomerMenuServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private RepairServiceDAO dao = new RepairServiceDAO();
	String serviceRequest;
	private HttpSession session;
	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		session = request.getSession(true);
		LoggedSubject loggedSubject = (LoggedSubject) session.getAttribute("loggedSubject");
		if(loggedSubject == null || loggedSubject.isCustomer()) {
			RequestDispatcher dispatcher = request.getRequestDispatcher("error.jsp");
			dispatcher.forward(request, response);
		} else {
			serviceRequest = request.getParameter("serviceRequest");
			Customer[] customerList = dao.getCustomerList();
			request.setAttribute("customerList", customerList);
			FileLogger.logMessage("Forwarded to customerMenu.jsp by CustomerMenuServlet.");
			RequestDispatcher dispatcher = request.getRequestDispatcher("customerMenu.jsp");
			dispatcher.forward(request, response);
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String customerName = request.getParameter("name");
		String actionButton = request.getParameter("actionButton");
		ServiceOrder currentServiceOrder = (ServiceOrder) session.getAttribute("currentServiceOrder");
		FileLogger.logMessage("Actions by ServiceOrderServlet.doPost().");
		if(actionButton != null)
			if(actionButton.equals("Search")) {
				if(customerName != null && customerName.equals(""))
					response.sendRedirect("./customerMenu");
				else if(customerName != null) {
					Customer[] sortedCustomerList = dao.getCustomerList(customerName);
					request.setAttribute("sortedCustomerList", sortedCustomerList);
					System.out.println(sortedCustomerList.length);
					FileLogger.logMessage("Forwarded to customerMenu.jsp by CustomerSearchServlet.doPost().");
					RequestDispatcher dispatcher = request.getRequestDispatcher("customerMenu.jsp");
					dispatcher.forward(request, response);
				}
			}
//			else if(actionButton.length() >= 14 && actionButton.substring(0, 14).equals("changeCustomer")){
//				dao.deleteServicePart(actionButton.substring(10), currentServiceOrder.getService_order());
//				response.sendRedirect(String.format("./serviceOrder?serviceRequest=%d", currentServiceOrder.getService_request_fk()));
//			}
		
	}

}
